import jwt
from flask import Request,json
__key__ = 'wocaonimade'
__algorithm__ = 'HS256'
def cerateToken(d:str):
    d = json.dumps(d)
    return jwt.encode(json.loads(d),__key__,__algorithm__)

def setKey(k:str):
    __key__ = k
    
def authToken(token:str):
    if(type(token)!=str):
        return False
    try:
        return jwt.decode(token,__key__,__algorithm__)
    except:
        return False

def authToken2(req:Request):
    token = req.headers.get("Authorization")
    if(token == None):
        return None
    info:dict = authToken(token)
    if(info == None):
        return None
    return info

def getUserId(req:Request):
    return req.environ.get("UID")

